//
//  NetWorkModel.m
//  SmartCoin
//
//  Created by apple on 2018/3/8.
//  Copyright © 2018年 Cynic. All rights reserved.
//

#import "NetWorkModel.h"
#import "sys/utsname.h"
#define weatherappkey @"8a41f33436154d5b83125817c66c9aa9"
#define posterhead @"gs-cloud-web-member"

@interface NetWorkModel()
@property (nonatomic ,retain)AFHTTPSessionManager *manager;
@end

@implementation NetWorkModel


-(instancetype)init{
    self = [super init];
    if (!_manager) {
        _manager = [AFHTTPSessionManager manager];
        //        _manager.responseSerializer.acceptableContentTypes =  [NSSet setWithObjects:@"application/json", @"text/json", @"text/javascript", @"text/html",@"text/plain",nil];
        //        [_manager.requestSerializer setValue:@"application/x-www-form-urlencoded; charset=utf-8" forHTTPHeaderField:@"Content-Type"];
        
    }
    return self;
}

-(void)AccessNetWorkWithURL:(NSString *)Parturl WithData:( id  _Nullable )data success:(_Nonnull SuccessAccess)success Fail:(_Nonnull FailAccess)failure isPost:(BOOL)isPost{
    NSString *url = @"";
    //    if ([Parturl containsString:@"http"]) {
    //
    //    }
    //    else{
    //        Parturl = [kBaseURL stringByAppendingString:Parturl];
    //    }
    url = [Parturl stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
    NSString *token = @"";
    if (cxsingler.loginrequest[@"token"]) {
        token = cxsingler.loginrequest[@"token"];
    }
    if ([url containsString:newSellerBase_Url]) {
        if (![data objectForKey:@"app_name"]) {
            [data setObject:@"smatcoin" forKey:@"app_name"];
            NSDictionary *infoDictionary = [[NSBundle mainBundle] infoDictionary];
            [data setObject:[infoDictionary objectForKey:@"CFBundleShortVersionString"] forKey:@"app_version"];
            [data setObject:@"iOS" forKey:@"platform"];
            [data setObject:[self deviceVersion] forKey:@"device"];
            [data setObject:[NSString stringWithFormat:@"%u",(arc4random() % 89999999) + 10000000] forKey:@"nonce"];
            [data setObject:[self currentTimeStr] forKey:@"ts"];
        }
        if ([data isKindOfClass:[NSDictionary class]] && ![data objectForKey:@"sign"]) {
        NSString *parmastr = @"";
            NSMutableArray *keys  = [[data allKeys] mutableCopy];
            keys = [[keys sortedArrayUsingComparator:^NSComparisonResult(id  _Nonnull obj1, id  _Nonnull obj2) {
                if (strcmp([obj1 UTF8String], [obj2 UTF8String]) > 0) {
                    return NSOrderedDescending;
                }
                else{
                    return NSOrderedAscending;
                }
            }] mutableCopy];
            for (NSInteger index = 0; index < keys.count; index++) {
                parmastr  = [parmastr stringByAppendingString:[NSString stringWithFormat:@"%@%@",keys[index],data[keys[index]]]];
            }
            parmastr = [newSeller_key stringByAppendingString:parmastr];
            parmastr = [parmastr stringByAppendingString:newSeller_key];
//            parmastr = [parmastr stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
           
            parmastr = [[Utils getMd5_32Bit:parmastr] lowercaseString];
//            parmastr = @"pKDEOgiPR1BO9L2482Z8Dbh5vBag2AWv";
            [data setObject:parmastr forKey:@"sign"];
        }
    }
    [_manager.requestSerializer setValue:token  forHTTPHeaderField:@"token"];
    if (isPost) {
        [_manager POST:url parameters:data progress:^(NSProgress * _Nonnull uploadProgress) {
            
        } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
            success(responseObject);
        } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
            failure(error);
        }];
    }
    else{
        [_manager GET:url parameters:data progress:^(NSProgress * _Nonnull uploadProgress) {
            
        } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
            success(responseObject);
        } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
            failure(error);
        }];
    }
}

#pragma mark---------------------------------------商户
#pragma mark--登录
-(void)loginwithUserAccount:(NSString *)acount withPass:(NSString *)secret{
    NSDictionary *parma = @{@"account":acount,@"password":secret};
    NSLog(@"%@",EmployerLogin_URL);
    [self AccessNetWorkWithURL:EmployerLogin_URL WithData:parma success:^(id responseObject) {
        if ([responseObject[@"code"] integerValue] == 200) {
            cxsingler.loginrequest = [responseObject[@"data"] mutableCopy];
            _sendMessage(@"success");
        }
        else{
            _sendMessage(responseObject[@"data"]);
        }
    } Fail:^(NSError *erro) {
        
    } isPost:YES];
}

#pragma mark--获取门户金额
-(void)getStoreDetailMoney{
    NSCalendar *calendar = [NSCalendar currentCalendar];
    NSDate *now = [NSDate date];
    NSDateComponents *components = [calendar components:NSCalendarUnitYear|NSCalendarUnitMonth|NSCalendarUnitDay fromDate:now];
    components.minute = 0;
    components.second = 0;
    NSDate *startDate = [calendar dateFromComponents:components];
    NSInteger timeint = [startDate timeIntervalSince1970];
    NSString *timeStr = [NSString stringWithFormat:@"%ld",timeint];
    timeStr = [timeStr stringByAppendingString:@"000"];
    //    timeStr = [NSString stringWithFormat:@"%ld",(long)([startDate timeIntervalSince1970] - 60 * 60 * 24 * 7) * 1000];
    NSString *AccountID = cxsingler.loginrequest[@"admin"][@"shop_id"];
    //    NSString *endstr =
    //    ,@"beginDateStr": timeStr
    NSDictionary *parma = @{@"shopId":AccountID,@"beginDateStr": timeStr};
    NSLog(@"%@",EmployerStoreInfo_URL);
    [self AccessNetWorkWithURL:EmployerStoreInfo_URL WithData:parma success:^(id responseObject) {
        if ([responseObject[@"code"] integerValue] == 200) {
            cxsingler.DicData = [responseObject[@"data"] mutableCopy];
            _sendMessage(@"success");
        }
        else{
            _sendMessage(responseObject[@"msg"]);
        }
    } Fail:^(NSError *erro) {
        _sendMessage(@"资金信息请求失败");
    } isPost:NO];
}

#pragma mark--获取门户明细列表
-(void)getStoreDetailBuessesswithpage:(NSInteger)CurrentPgae withOnlyToday:(BOOL)OnlyToday{
    NSCalendar *calendar = [NSCalendar currentCalendar];
    NSDate *now = [NSDate date];
    NSDateComponents *components = [calendar components:NSCalendarUnitYear|NSCalendarUnitMonth|NSCalendarUnitDay fromDate:now];
    NSDate *startDate = [calendar dateFromComponents:components];
    NSString *star = [NSString stringWithFormat:@"%ld",(long)[startDate timeIntervalSince1970]];
    star = [star stringByAppendingString:@"000"];
    
    //    star = [NSString stringWithFormat:@"%ld",(long)([startDate timeIntervalSince1970] - 60 * 60 * 24 * 7) * 1000];
    NSString *end = [NSString stringWithFormat:@"%ld",((long)[startDate timeIntervalSince1970] + 86400 )];
    end = [end stringByAppendingString:@"000"];
    NSString *AccountID = cxsingler.loginrequest[@"admin"][@"shop_id"];
    NSDictionary *parma = @{
                            @"pageNumber":[NSNumber numberWithInteger:CurrentPgae],
                            @"shopId":AccountID,
                            @"beginDateStr":star,
                            @"endDateStr":end
                            };
    if(!OnlyToday){
        parma = @{
                  @"pageNumber":[NSNumber numberWithInteger:CurrentPgae],
                  @"shopId":AccountID,
//                  @"beginDateStr":star,
//                  @"endDateStr":end
                  };
    }
    [self AccessNetWorkWithURL:StorePageTableviewData_URL WithData:parma success:^(id responseObject) {
        if ([responseObject[@"code"] integerValue] == 200) {
            cxsingler.ArraryData = [responseObject[@"data"][@"list"] mutableCopy];
//            if ([responseObject[@"data"][@"totalCount"] integerValue] == 0) {
//                _sendMessage(@"暂无明细数据");
//            }
//            else{
                _sendMessage(@"success");
//            }
        }
        else{
            _sendMessage(responseObject[@"msg"]);
        }
    } Fail:^(NSError *erro) {
        _sendMessage(@"明细记录获取失败");
    } isPost:NO];
}

#pragma mark--获取积分明细列表
-(void)getDetailintegralwithpage:(NSInteger)CurrentPgae{
    NSCalendar *calendar = [NSCalendar currentCalendar];
    NSDate *now = [NSDate date];
    NSDateComponents *components = [calendar components:NSCalendarUnitYear|NSCalendarUnitMonth|NSCalendarUnitDay fromDate:now];
    NSDate *startDate = [calendar dateFromComponents:components];
    NSString *star = [NSString stringWithFormat:@"%ld",(long)[startDate timeIntervalSince1970]];
    star = [star stringByAppendingString:@"000"];
    NSString *end = [NSString stringWithFormat:@"%ld",((long)[startDate timeIntervalSince1970] + 86400 )];
    end = [end stringByAppendingString:@"000"];
    NSString *AccountID = cxsingler.loginrequest[@"admin"][@"shop_id"];
    NSDictionary *parma = @{
                            @"beginDateStr":star,
                            @"endDateStr":end,
                            @"search":@"in"
                            };
    [self AccessNetWorkWithURL:[NSString stringWithFormat:@"%@%@/%ld",AccumulationInfo_URL,AccountID,CurrentPgae] WithData:parma success:^(id responseObject) {
        if ([responseObject[@"code"] integerValue] == 200) {
            cxsingler.ArraryData = [responseObject[@"data"][@"flows"] mutableCopy];
            if ([responseObject[@"data"][@"totalCount"] integerValue] == 0) {
                _sendMessage(@"暂无明细数据");
            }
            else{
                _sendMessage(@"success");
            }
        }
        else{
            _sendMessage(responseObject[@"msg"]);
        }
    } Fail:^(NSError *erro) {
        _sendMessage(@"积分明细获取失败");
    } isPost:NO];
}

#pragma mark--配置获取
-(void)getSettinginfo{
    NSString *url = [NSString stringWithFormat:@"%@/app/ios/getVersion",baseUrl];
    [self AccessNetWorkWithURL:url WithData:nil success:^(id responseObject) {
        if ([responseObject[@"code"] integerValue] == 200) {
            if ([responseObject[@"data"] integerValue] == 0) {
                cxsingler.allowed = NO;
            }
            else if ([responseObject[@"data"] integerValue] == 1){
                cxsingler.allowed = YES;
            }
        }
        else{
            _sendMessage(responseObject[@"status"]);
        }
    } Fail:^(NSError *erro) {
        _sendMessage(@"failed");
    } isPost:NO];
}


#pragma mark--获取商户信息
-(void)getSllerInfo{
    [self AccessNetWorkWithURL:[NSString stringWithFormat:@"%@%@",MerchatInfo_URL,cxsingler.loginrequest[@"admin"][@"shop_id"]] WithData:nil success:^(id responseObject) {
        if ([responseObject[@"code"] integerValue] == 200) {
            cxsingler.DicData = [responseObject[@"data"] mutableCopy];
            _sendMessage(@"success");
        }
        else{
            _sendMessage(responseObject[@"msg"]);
        }
    } Fail:^(NSError *erro) {
        _sendMessage(@"商户信息获取失败");
    } isPost:NO];
}

#pragma mark--静音否
-(void)changeringstatus:(BOOL)ring{
    NSDictionary *parma = @{@"state":[NSString stringWithFormat:@"%d",ring]};
    [self AccessNetWorkWithURL:SetNotificationSpeaker_URL WithData:parma success:^(id responseObject) {
        if ([responseObject[@"code"] integerValue] == 200) {
            _sendMessage(@"success");
        }
        else{
            _sendMessage(responseObject[@"msg"]);
        }
    } Fail:^(NSError *erro) {
        _sendMessage(@"闹钟模式修改失败");
    } isPost:YES];
}


#pragma supper     -------------商户端
#pragma mark--微信登录
-(void)wexinLogin:(NSString *)openID{
    //    NSString *url = [NSString stringWithFormat:@"%@/employee/wechat/",baseUrl];
    NSString *url = [NSString stringWithFormat:@"%@/employee/loginByUnionid",baseUrl];
    NSDictionary *parma = @{@"unionid":openID};
    [self AccessNetWorkWithURL:url WithData:parma success:^(id responseObject) {
        if ([responseObject[@"status"] isEqual:@"数据不存在(40401)"]) {
            _sendMessage(@"newin");
        }
        else if([responseObject[@"code"] integerValue] == 200){
            cxsingler.loginrequest = [responseObject[@"data"] mutableCopy];
            _sendMessage(@"success");
        }
        else{
            
        }
    } Fail:^(NSError *erro) {
        _sendMessage(@"微信登录失败");
    } isPost:YES];
}


#pragma mark--获取手机验证码
-(void)getMessageCodeByPhone:(NSString *)phone{
    NSString *url = [NSString stringWithFormat:@"%@/employee/getCode",baseUrl];
    NSDictionary *parmas = @{@"mobile":phone,@"validCode":@""};
    [self AccessNetWorkWithURL:url WithData:parmas success:^(id responseObject) {
        
    } Fail:^(NSError *erro) {
        
    } isPost:YES];
}

#pragma mark--手机激活
-(void)connectWithOpenid:(NSString *)Openid Code:(NSString *)code{
    NSString *url = [NSString stringWithFormat:@"%@/employee/mobile/",baseUrl];
    NSDictionary *parmas = @{@"openId":Openid,@"validCode":code};
    [self AccessNetWorkWithURL:url WithData:parmas success:^(id responseObject) {
        
    } Fail:^(NSError *erro) {
        
    } isPost:YES];
}

#pragma mark--获取员工所在商户列表
-(void)getBossInfoListByUser:(NSString *)userID{
    NSString *url = [NSString stringWithFormat:@"%@/employee/shopList/",baseUrl];
    NSDictionary *parmas = @{@"mobile":userID};
    [self AccessNetWorkWithURL:url WithData:parmas success:^(id responseObject) {
        if ([responseObject[@"code"] integerValue] == 200) {
            cxsingler.ArraryData = [responseObject[@"data"] mutableCopy];
            _sendMessage(@"success");
        }
        else{
            _sendMessage(responseObject[@"msg"]);
        }
    } Fail:^(NSError *erro) {
        _sendMessage(@"商户列表获取失败");
    } isPost:NO];
}

#pragma mark--获取员工账户信息
-(void)getUserMoneyinfoByUser:(NSString *)userID{
    NSString *url = [NSString stringWithFormat:@"%@/employee/%@/",baseUrl,userID];
    //    NSDictionary *parmas = @{@"staffId":userID};
    [self AccessNetWorkWithURL:url WithData:nil success:^(id responseObject) {
        if ([responseObject[@"code"] integerValue] == 200) {
            cxsingler.DicData = [responseObject[@"data"] mutableCopy];
            _sendMessage(@"success");
        }
        else{
            _sendMessage(responseObject[@"status"]);
        }
    } Fail:^(NSError *erro) {
        _sendMessage(@"基本信息获取失败");
    } isPost:NO];
}

#pragma mark--获取员工分红明细
-(void)getUserBonusListByUser:(NSString *)userID withPage:(NSInteger)page{
    NSString *url = [NSString stringWithFormat:@"%@/employee/%@/bonus/",baseUrl,cxsingler.staffid];
    NSDictionary *dic = @{@"pageSize":@"10",@"pageNumber":[NSString stringWithFormat:@"%ld",page]};
    [self AccessNetWorkWithURL:url WithData:dic success:^(id responseObject) {
        if ([responseObject[@"code"] integerValue ] == 200) {
            NSLog(@"员工分红明细%@",responseObject);
            cxsingler.ArraryData = [responseObject[@"data"] mutableCopy];
            _sendMessage(@"success");
        }
        else{
            _sendMessage(responseObject[@"status"]);
        }
    } Fail:^(NSError *erro) {
        _sendMessage(@"员工分红明细请求失败");
    } isPost:NO];
}

#pragma mark--获取员工提现明细
-(void)getUserCashOutDetailListByUser:(NSString *)userID withPage:(NSInteger)page{
    NSString *url = [NSString stringWithFormat:@"%@/employee/%@/withdrawal/",baseUrl,cxsingler.staffid];
    NSDictionary *parmas = @{@"pageSize":@"10",@"pageNumber":[NSString stringWithFormat:@"%ld",page]};
    [self AccessNetWorkWithURL:url WithData:parmas success:^(id responseObject) {
        NSLog(@"%@",responseObject);
        if ([responseObject[@"code"] integerValue ] == 200) {
            cxsingler.ArraryData = [responseObject[@"data"] mutableCopy];
            _sendMessage(@"success");
        }
        else{
            _sendMessage(responseObject[@"status"]);
        }
    } Fail:^(NSError *erro) {
        _sendMessage(@"员工提现明细请求失败");
    } isPost:NO];
    
}

#pragma mark--申请提现
-(void)CashOutByUser:(NSString *)userID withMoney:(NSString *)money{
    NSString *url = [NSString stringWithFormat:@"%@/employee/%@/presentApplication/",baseUrl,cxsingler.staffid];
    NSDictionary *parmas = @{@"amount":money};
    [self AccessNetWorkWithURL:url WithData:parmas success:^(id responseObject) {
        if ([responseObject[@"code"] integerValue] == 200) {
            _sendMessage(@"success");
        }
        else{
            _sendMessage(responseObject[@"status"]);
        }
    } Fail:^(NSError *erro) {
        _sendMessage(@"申请提现失败");
    } isPost:YES];
}


#pragma mark--获取商户设置
-(void)getMechantSettingByshopID:(NSString *)shopID{
    NSString *url = [NSString stringWithFormat:@"%@/shopSetting/%@",CZurl,shopID];
    //    NSDictionary *parma = [NSDictionary dictionaryWithObjectsAndKeys:shopID,@"shopId", nil];
    [self AccessNetWorkWithURL:url WithData:nil success:^(id responseObject) {
        if ([responseObject[@"code"] integerValue] == 200) {
            cxsingler.MerchantSetting = [NSMutableDictionary dictionaryWithDictionary:responseObject[@"data"]];
            _sendMessage(@"success");
        }
        else{
            _sendMessage(responseObject[@"msg"]);
        }
    } Fail:^(NSError *erro) {
        _sendMessage(@"商户设置信息获取失败");
    } isPost:NO];
}


#pragma supper -----------------------------新增商户首页部分
#pragma mark--获取订单列表
-(void)NewSellerStoreGetOrderlistByshopID:(NSString *)shopID WithPage:(NSInteger)currentpage withOnlyToday:(BOOL)onlyToday;{
//    shopID = @"20";
    NSString *url = [NSString stringWithFormat:@"%@public/order.php/trade/tradeList",newSellerBase_Url];
    NSMutableDictionary *parma = [NSMutableDictionary dictionaryWithObjectsAndKeys:shopID,@"shop_id",[NSString stringWithFormat:@"%ld",currentpage],@"page",@"20",@"page_size",@"complete", @"tag",nil];//@"0-1527990378",@"period"
    if (!onlyToday) {
        parma = [NSMutableDictionary dictionaryWithObjectsAndKeys:shopID,@"shop_id",[NSString stringWithFormat:@"%ld",currentpage],@"page",@"20",@"page_size",@"complete", @"tag",[NSString stringWithFormat:@"0-%ld",(long)[[NSDate dateWithTimeIntervalSinceNow:0] timeIntervalSince1970]],@"period",nil];
        NSLog(@"timming = %ld",(long)[[NSDate dateWithTimeIntervalSinceNow:0] timeIntervalSince1970]);
    }
    [self AccessNetWorkWithURL:url WithData:parma success:^(id responseObject) {
        if ([responseObject[@"code"] integerValue] == 200) {
            cxsingler.ArraryData = [responseObject[@"data"][@"data"] mutableCopy];
            _sendMessage(@"success");
        }
        else{
            _sendMessage(responseObject[@"msg"]);
        }
    } Fail:^(NSError *erro) {
        _sendMessage(@"订单列表获取失败");
    } isPost:NO];
}

#pragma mark--营业额获取
-(void)NewSellerStoreMoneyinfowithShopID:(NSString *)ShopID{
    NSString *url = [NSString stringWithFormat:@"%@public/order.php/trade/revenue",newSellerBase_Url];
//    ShopID = @"20";
    NSMutableDictionary *parma = [NSMutableDictionary dictionaryWithObjectsAndKeys:ShopID,@"shop_id", nil];
    [self AccessNetWorkWithURL:url WithData:parma success:^(id responseObject) {
        if ([responseObject[@"code"] integerValue] == 200) {
            cxsingler.DicData = [responseObject[@"data"] mutableCopy];
            _sendMessage(@"success");
        }
        else{
            _sendMessage(responseObject[@"msg"]);
        }
    } Fail:^(NSError *erro) {
        _sendMessage(@"营业额获取失败");
    } isPost:NO];
}


- (NSString*)dictionaryToJson:(NSDictionary *)dic
{
    NSError *parseError = nil;
    NSData *jsonData = [NSJSONSerialization dataWithJSONObject:dic options:NSJSONWritingPrettyPrinted error:&parseError];
    return [[NSString alloc] initWithData:jsonData encoding:NSUTF8StringEncoding];
}
- (NSString*)deviceVersion
{
    struct utsname systemInfo;
    uname(&systemInfo);
    NSString * deviceString = [NSString stringWithCString:systemInfo.machine encoding:NSUTF8StringEncoding];
    //iPhone
    if ([deviceString isEqualToString:@"iPhone1,1"])    return @"iPhone 1G";
    if ([deviceString isEqualToString:@"iPhone1,2"])    return @"iPhone 3G";
    if ([deviceString isEqualToString:@"iPhone2,1"])    return @"iPhone 3GS";
    if ([deviceString isEqualToString:@"iPhone3,1"])    return @"iPhone 4";
    if ([deviceString isEqualToString:@"iPhone3,2"])    return @"Verizon iPhone 4";
    if ([deviceString isEqualToString:@"iPhone4,1"])    return @"iPhone 4S";
    if ([deviceString isEqualToString:@"iPhone5,1"])    return @"iPhone 5";
    if ([deviceString isEqualToString:@"iPhone5,2"])    return @"iPhone 5";
    if ([deviceString isEqualToString:@"iPhone5,3"])    return @"iPhone 5C";
    if ([deviceString isEqualToString:@"iPhone5,4"])    return @"iPhone 5C";
    if ([deviceString isEqualToString:@"iPhone6,1"])    return @"iPhone 5S";
    if ([deviceString isEqualToString:@"iPhone6,2"])    return @"iPhone 5S";
    if ([deviceString isEqualToString:@"iPhone7,1"])    return @"iPhone 6 Plus";
    if ([deviceString isEqualToString:@"iPhone7,2"])    return @"iPhone 6";
    if ([deviceString isEqualToString:@"iPhone8,1"])    return @"iPhone 6s";
    if ([deviceString isEqualToString:@"iPhone8,2"])    return @"iPhone 6s Plus";
    if ([deviceString isEqualToString:@"iPhone8,4"])    return @"iPhone SE";
    if ([deviceString isEqualToString:@"iPhone9,1"])    return @"iPhone 7";
    if ([deviceString isEqualToString:@"iPhone9,3"])    return @"iPhone 7";
    if ([deviceString isEqualToString:@"iPhone9,2"])    return @"iPhone 7 Plus";
    if ([deviceString isEqualToString:@"iPhone9,4"])    return @"iPhone 7 Plus";
    if ([deviceString isEqualToString:@"iPhone8,1"])    return @"iPhone 8";
    if ([deviceString isEqualToString:@"iPhone8,4"])    return @"iPhone 8";
    if ([deviceString isEqualToString:@"iPhone8,2"])    return @"iPhone 8 Plus";
    if ([deviceString isEqualToString:@"iPhone8,5"])    return @"iPhone 8 Plus";
    if ([deviceString isEqualToString:@"iPhone8,3"])    return @"iPhone X";
    if ([deviceString isEqualToString:@"iPhone8,6"])    return @"iPhone X";
    　return deviceString;
}

//获取当前时间戳
- (NSString *)currentTimeStr{
    NSDate* date = [NSDate dateWithTimeIntervalSinceNow:0];//获取当前时间0秒后的时间
    NSTimeInterval time=[date timeIntervalSince1970];// *1000 是精确到毫秒，不乘就是精确到秒
    NSString *timeString = [NSString stringWithFormat:@"%.0f", time];
    return timeString;
}

#pragma mark ------------------------------新增积分商户首页部分
#pragma mark--积分商城资金信息
-(void)GetCodeStoreMoneyInfoByShopID:(NSString *)shopid{
    NSString *url = [NSString stringWithFormat:@"%@/app/stat",CodeStoreUrl];
    NSDictionary *parma = [NSDictionary dictionaryWithObjectsAndKeys:shopid,@"shopId", nil];
    [self AccessNetWorkWithURL:url WithData:parma success:^(id responseObject) {
        if ([responseObject[@"code"] integerValue] == 200) {
            cxsingler.DicData = [responseObject[@"data"][0] mutableCopy];
            _sendMessage(@"success");
        }
        else{
            _sendMessage(responseObject[@"msg"]);
        }
    } Fail:^(NSError *erro) {
        _sendMessage(@"分销商城营业信息请求失败");
    } isPost:NO];
}

#pragma mark--积分商城订单列表
-(void)GetCodeStoreListWithCurrentPage:(NSInteger)currentPage withShopID:(NSString *)shopid{
    NSString *url = [NSString stringWithFormat:@"%@/app/tradeList",CodeStoreUrl];
    NSDictionary *parma = [NSDictionary dictionaryWithObjectsAndKeys:shopid,@"shopId",[NSString stringWithFormat:@"%ld",currentPage],@"pageNumber",@"20",@"pageSize", nil];//@"0",@"beginDateStr"
    [self AccessNetWorkWithURL:url WithData:parma success:^(id responseObject) {
        if ([responseObject[@"code"] integerValue] == 200) {
            cxsingler.ArraryData = [responseObject[@"data"][@"list"] mutableCopy];
            _sendMessage(@"success");
        }
        else{
            _sendMessage(responseObject[@"msg"]);
        }
    } Fail:^(NSError *erro) {
        _sendMessage(@"分销商城列表获取失败");
    } isPost:NO];
}

#pragma mark--积分商城详情
-(void)GetDetailCodeStoreInfoByShopID:(NSString *)shopid{
    NSString *url = [NSString stringWithFormat:@"%@/app/tradeDetail",CodeStoreUrl];
    NSDictionary *parma = [NSDictionary dictionaryWithObjectsAndKeys:shopid,@"tradeId", nil];
    [self AccessNetWorkWithURL:url WithData:parma success:^(id responseObject) {
        if ([responseObject[@"code"] integerValue] == 200) {
            cxsingler.DicData = [responseObject[@"data"] mutableCopy];
            _sendMessage(@"success");
        }
        else{
            _sendMessage(responseObject[@"msg"]);
        }
    } Fail:^(NSError *erro) {
        _sendMessage(@"积分商城详情获取失败");
    } isPost:NO];
}


#pragma mark--获取segnum
-(void)getSegNum{
    NSString *url = @"https://distribution.xingchengfu.com/h5/estimateShopId";
    NSDictionary *parma = [NSDictionary dictionaryWithObjectsAndKeys:cxsingler.loginrequest[@"admin"][@"shop_id"],@"shopId", nil];
//    NSDictionary *parma = [NSDictionary dictionaryWithObjectsAndKeys:@"51",@"shopId", nil];//测试多seg
    [self AccessNetWorkWithURL:url WithData:parma success:^(id responseObject) {
        if([responseObject[@"code"] integerValue]  == 200){
            cxsingler.StrData = [NSMutableString stringWithFormat:@"%@",responseObject[@"data"]];
            _sendMessage(@"success");
        }
        else{
            _sendMessage(responseObject[@"msg"]);
        }
    } Fail:^(NSError *erro) {
        _sendMessage(@"登录发生错误");
    } isPost:NO];
}


#pragma mark--用户的红包领取记录
-(void)getUserGetRedBagHisByPage:(NSInteger)page withUserid:(NSString *)userID{
    NSString *url = [NSString stringWithFormat:@"%@/integral/user/red/packet/records/%@/%ld",RedbBagHeader,userID,page];
    [self AccessNetWorkWithURL:url WithData:[NSDictionary dictionary] success:^(id responseObject) {
        if ([responseObject[@"code"] integerValue] == 200) {
            cxsingler.ArraryData = [responseObject[@"data"] mutableCopy];
            _sendMessage(@"success");
        }
        else{
            _sendMessage(responseObject[@"msg"]);
        }
    } Fail:^(NSError *erro) {
        _sendMessage(@"请求失败");
    } isPost:NO];
}
///integral/user/red/packet/records/:user_id/:page
#pragma mark--商户的红包记录
-(void)getSellerSendRedBagHisByPage:(NSInteger)page withShopID:(NSString *)shopID{
    NSString *url = [NSString stringWithFormat:@"%@/integral/shop/red/packet/records/%@/%ld?page_size=10",RedbBagHeader,shopID,page];
    [self AccessNetWorkWithURL:url WithData:[NSDictionary dictionary] success:^(id responseObject) {
        if ([responseObject[@"code"] integerValue] == 200) {
            cxsingler.ArraryData = [responseObject[@"data"][@"record"] mutableCopy];
            _sendMessage(@"success");
        }
        else{
            _sendMessage(responseObject[@"msg"]);
        }
    } Fail:^(NSError *erro) {
        
    } isPost:NO];
}
#pragma mark--领取红包
-(void)getRedBagByID:(NSString *)bagID{
    NSString *url = [NSString stringWithFormat:@"%@/integral/shop/red/packet/get/:user_id/%@",RedbBagHeader,bagID];
    [self AccessNetWorkWithURL:url WithData:[NSDictionary dictionary] success:^(id responseObject) {
        
    } Fail:^(NSError *erro) {
        
    } isPost:NO];
}
#pragma mark--红包统计
-(void)TotallRedInfoByID:(NSString *)shopID{
    NSString *url = [NSString stringWithFormat:@"%@/integral/shop/red/packet/statistics/%@",RedbBagHeader,shopID];
    [self AccessNetWorkWithURL:url WithData:[NSDictionary dictionary] success:^(id responseObject) {
        if ([responseObject[@"code"] integerValue] == 200) {
            cxsingler.DicData = [responseObject[@"data"] mutableCopy];
            _sendMessage(@"success");
        }
        else{
            _sendMessage(responseObject[@"msg"]);
        }
    } Fail:^(NSError *erro) {
        _sendMessage(@"请求失败");
    } isPost:NO];
}
//http://{{host}}/integral/shop/red/packet/statistics/:shop_id
#pragma mark--红包详情
-(void)getRedBagDetailInfoByID:(NSString *)bagID{
    NSString *url = [NSString stringWithFormat:@"%@/integral/shop/red/packet/info/%@",RedbBagHeader,bagID];
    [self AccessNetWorkWithURL:url WithData:[NSDictionary dictionary] success:^(id responseObject) {
        if ([responseObject[@"code"] integerValue] == 200) {
            cxsingler.DicData = [responseObject[@"data"] mutableCopy];
            _sendMessage(@"success");
        }
        else{
            _sendMessage(responseObject[@"msg"]);
        }
    } Fail:^(NSError *erro) {
        _sendMessage(@"请求失败");
    } isPost:NO];
}
#pragma mark--单个红包领取记录
-(void)getSingleRedBagGetterHisByID:(NSString *)bagID{
    NSString *url = [NSString stringWithFormat:@"%@/integral/shop/red/packet/record/%@",RedbBagHeader,bagID];
    [self AccessNetWorkWithURL:url WithData:[NSDictionary dictionary] success:^(id responseObject) {
        if ([responseObject[@"code"] integerValue] == 200) {
            cxsingler.DicData = responseObject[@"data"];
            _sendMessage(@"success");
        }
        else{
            _sendMessage(responseObject[@"msg"]);
        }
    } Fail:^(NSError *erro) {
        _sendMessage(@"请求失败");
    } isPost:NO];
}
#pragma mark--派发红包
//bagtype 0 随机 random ： 1 普通 normal
-(void)SendOutRedBagsWithType:(NSInteger)BagType withPrice:(NSString *)PriceNum withCount:(NSString *)countNum withMessage:(NSString *)message withShopID:(NSString *)shopID{
    NSString *url = [NSString stringWithFormat:@"%@/integral/shop/red/packet/send/%@",RedbBagHeader,shopID];
    NSDictionary *parma = @{
                            @"type":BagType == 0 ?  @"normal": @"random",
                                @"amount_or_unit":[NSString stringWithFormat:@"%@",PriceNum],
                                @"count":[NSString stringWithFormat:@"%@",countNum],
                                @"message":message
                            };
    [self AccessNetWorkWithURL:url WithData:parma success:^(id responseObject) {
        if ([responseObject[@"code"] integerValue] == 200) {
            cxsingler.StrData = responseObject[@"data"];
//            cxsingler.DicData = [responseObject[@"data"] mutableCopy];
            _sendMessage(@"success");
        }
        else{
            _sendMessage(responseObject[@"msg"]);
        }
    } Fail:^(NSError *erro) {
        _sendMessage(@"处理失败");
    } isPost:YES];
}
@end
